/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package members;
import com.opensymphony.xwork2.ActionContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.Session;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Gjun
 */
public class VipCenter {
  
    private int ID;
   private memberItem result;

   private double sum;
     private HashMap<String, Object> rr;
     private ArrayList<HashMap<String, Object>> rt;
    
    
    public VipCenter() {
    }

    public String execute() throws Exception {
        String mes="fail";
//會員中心 
         HttpServletRequest request=ServletActionContext.getRequest();   
         Map session=(Map)ActionContext.getContext().get(ActionContext.SESSION);
          if(session.get("User")==null)
         {
             mes="fail";
         }else if(session.get("User")!=null){
        
         setID(Integer.parseInt(request.getSession().getAttribute("會員編號").toString()));

        NWDB.NWDB mydb = new NWDB.NWDB();
        NWDB.IMember im = new NWDB.ImplMember(mydb.getConnection());
        mydb.setIMember(im);
       
//--撈會員基本資料        
        HashMap<String, Object> rs = mydb.GetMemberStatus(getID());
        
         result = new memberItem();  
         result.setId(getID());
         result.setAccount(rs.get("Account").toString());
         result.setName(rs.get("Name").toString());
         result.setSex(rs.get("Sex").toString());
         if(result.getSex()=="true")
         {
             result.setSex("男生");
         }else if(result.getSex()=="false"){
             result.setSex("女生");
         }else{
         result.setSex("");
         } 
         result.setBirth(rs.get("Birth").toString());
         result.setAddress(rs.get("Address").toString());
         result.setPhone(rs.get("Phone").toString());
         result.setEmail(rs.get("Email").toString());
    

//--撈會員訂購紀錄 
         rt=mydb.GetMemberbuylist(getID());
         for(int i=0;i<rt.size();i++){
            sum+= Double.parseDouble(rt.get(i).get("小計").toString());
         }

         if(getResult()!=null){
                if(getRt()!=null)
                    mes="success";
                else
                    mes="fail";
         }else {
                mes="fail";
         }
         }
        return mes;
  }

    public String update() throws Exception {
        String mes="fail";
   
         HttpServletRequest request=ServletActionContext.getRequest();   
         Map session=(Map)ActionContext.getContext().get(ActionContext.SESSION);
          if(session.get("User")==null)
         {
             mes="fail";
         }else if(session.get("User")!=null){
        
         setID(Integer.parseInt(request.getSession().getAttribute("會員編號").toString()));

        NWDB.NWDB mydb = new NWDB.NWDB();
        NWDB.IMember im = new NWDB.ImplMember(mydb.getConnection());
        mydb.setIMember(im);    
        String s1=request.getParameter("Password").toString();
        String s2=request.getParameter("Name").toString();
        String s3=request.getParameter("Phone").toString();
        String s4=request.getParameter("Address").toString();
        String s5=request.getParameter("Email").toString();
             mydb.updateMember(getID(),s1,s2,s3,s4,s5);
        /*
        session.put("Id",getID());
        session.put("Password",s1);
        session.put("Name",s2);
        session.put("Phone",s3);
        session.put("Address",s4);
        session.put("Email",s5);
       */
        mes="success";
        
        
        //--撈會員基本資料        
        HashMap<String, Object> rs = mydb.GetMemberStatus(getID());
        
         result = new memberItem();  
         result.setId(getID());
         result.setAccount(rs.get("Account").toString());
         result.setName(rs.get("Name").toString());
         result.setSex(rs.get("Sex").toString());
         if(result.getSex()=="true")
         {
             result.setSex("男生");
         }else if(result.getSex()=="false"){
             result.setSex("女生");
         }else{
         result.setSex("");
         } 
         result.setBirth(rs.get("Birth").toString());
         result.setAddress(rs.get("Address").toString());
         result.setPhone(rs.get("Phone").toString());
         result.setEmail(rs.get("Email").toString());
    

//--撈會員訂購紀錄 
         rt=mydb.GetMemberbuylist(getID());
         for(int i=0;i<rt.size();i++){
            sum+= Double.parseDouble(rt.get(i).get("小計").toString());

         }

         if(getResult()!=null){
                if(getRt()!=null)
                    mes="success";
                else
                    mes="fail";
         }else {
                mes="fail";
         }
        }
        return mes;
    } 
    
    /**
     * @return the result
     */
    public memberItem getResult() {
        return result;
    }

    /**
     * @param result the result to set
     */
    public void setResult(memberItem result) {
        this.result = result;
    }

    /**
     * @return the ID
     */
    public int getID() {
        return ID;
    }

    /**
     * @param ID the ID to set
     */
    public void setID(int ID) {
        this.ID = ID;
    }


    /**
     * @return the rt
     */
    public ArrayList<HashMap<String, Object>> getRt() {
        return rt;
    }

    /**
     * @param rt the rt to set
     */
    public void setRt(ArrayList<HashMap<String, Object>> rt) {
        this.rt = rt;
    }

    /**
     * @return the sum
     */
    public double getSum() {
        return sum;
    }

    /**
     * @param sum the sum to set
     */
    public void setSum(double sum) {
        this.sum = sum;
    }

    /**
     * @return the rr
     */
    public HashMap<String, Object> getRr() {
        return rr;
    }

    /**
     * @param rr the rr to set
     */
    public void setRr(HashMap<String, Object> rr) {
        this.rr = rr;
    }

    /**
     * @return the mi
     */
}